clear all
set more off

do "set_path.do"

***** Fig 2: 5-Year Median Government CIP *****

use "${datadir}cip_all_jun2025.dta", clear

rangestat cip_mean = cip_govt, interval(date -13 0) by(currency tenor)
keep group currency tenor date cip_mean

drop if date < mdy(1,1,2004)

* Fill-in data for missing date so we don't have a median with too few observations
reshape wide cip_mean, i(group tenor date) j(currency) string
reshape long cip_mean, i(group tenor date) j(currency) string
sort group currency tenor date
bys group currency tenor: replace cip_mean = cip_mean[_n-1] if missing(cip_mean) & !missing(cip_mean[_n-1])

collapse (median) cip_mean, by(group tenor date)

twoway (tsline cip_mean if group=="eme" & tenor=="5y", cmissing(no) lwidth(medthin) lcolor(blue) lpattern(dash)  legend(label(1 "EM"))) ///
	   (tsline cip_mean if group=="g10" & tenor=="5y", cmissing(no) lwidth(medthin) lcolor(red)  lpattern(solid) legend(label(2 "G10"))), ///	   
		yline(0, lwidth(vthin) lcolor(black) lstyle(solid)) ylabel(-100(50)250,labsize(small) nogrid) xlabel(#21, labsize(small) format(%tdCY) angle(45) nogrid) ///
		xtitle("") ytitle("Basis Points", size(small)) legend(size(small) region(lstyle(none))) ///
		leg(pos(6) rows(1)) ///
		graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) ///
		subtitle(, size(small) ring(0) pos(1) nobexpand) title("Median Five-Year Government CIP Deviations", size(median)) note("")
		
graph export "${outdir}figures/cip_median.pdf", replace
